package com.fht.service.shop.impl;

import com.fht.entity.PurchaseOrders;
import com.fht.entity.ShopTable;
import com.fht.from.ShopFrom;
import com.fht.mapper.shop.ShopTableMapper;
import com.fht.service.shop.ShopTableService;
import com.fht.vo.LayUITableVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ShopTableServiceImpl implements ShopTableService {
    @Resource
    public ShopTableMapper shopTableMapper;

    @Override
    public LayUITableVO<List<ShopTable>> findPage(Map<String, Object> map) {
        ShopFrom from = (ShopFrom) map.get("from");

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        Example example = new Example(ShopTable.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("merchantsId",from.getMerchantsId());

        if (null!=from.getFloorNum() && from.getFloorNum() !=0 ){
            c.andLike("floorNum", String.valueOf(from.getFloorNum()));
        }

        List<ShopTable> list = shopTableMapper.selectByExample(example);

        for (ShopTable shopTable : list) {
            if (shopTable.getStatus() == 1){
                shopTable.setStatusString("客满");
            }else {
                shopTable.setStatusString("无客");
            }
        }

        PageInfo<ShopTable> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<ShopTable>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;

    }

    @Override
    public ShopTable selectById(String id) {
        return shopTableMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(ShopTable shopTable) {
        shopTableMapper.updateByPrimaryKeySelective(shopTable);
    }

    @Override
    @Transactional
    public void save(ShopTable shopTable) {
        shopTableMapper.insertSelective(shopTable);
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        shopTableMapper.deleteByPrimaryKey(id);

    }
}
